package Action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.coursedao;
import Entity.Course;
import mapper.Coursemapper;

/**
 * Servlet implementation class searchCourseServlet
 */
@WebServlet("/searchCourseServlet")
public class searchCourseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public searchCourseServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String  search = request.getParameter("search");
		int sum;
        int page = Integer.parseInt(request.getParameter("page"));
        Coursemapper cm = new coursedao();
        List<Course> courses=null;
        if(search==null){
        	courses =cm.searchAllCourses(page);
        	sum =courses.size();
        	
        }
        else{
        	courses = cm.searchCourses(page, search);
        	sum =courses.size();
        }
        request.setAttribute("courses",courses);
        if(sum%10==0)
        {
        	sum=sum/10;
        }
        else
        	sum=sum/10+1;
        System.out.println(page);
        request.setAttribute("sum",sum);
        request.setAttribute("courses", courses);
        request.setAttribute("cpage", page);
        request.getRequestDispatcher("search_class.jsp").forward(request,response);
	}

}
